package com.tsing.service.impl;

import com.tsing.mapper.PostDetailsMapper;
import com.tsing.mapper.impl.PostDetailsMapperImpl;
import com.tsing.pojo.entity.TlDetails;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.TlUser;
import com.tsing.pojo.vo.CommentDetailsVO;
import com.tsing.service.PostDetailsService;

import java.util.ArrayList;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/12 10:17
 */
public class PostDetailsServiceImpl implements PostDetailsService {
    private final PostDetailsMapper postDetailsMapper = new PostDetailsMapperImpl();
    @Override
    public List<CommentDetailsVO> list(Integer postId, Integer userId) {
        //查询评论数据
        List<TlDetails> tlDetails = postDetailsMapper.list(postId);
        List<CommentDetailsVO> list = new ArrayList<>();
        for (TlDetails tlDetail : tlDetails) {
            //查询发评论的人的信息
            TlUser user = postDetailsMapper.userById(tlDetail.getUserId());
            CommentDetailsVO commentDetailsVO =new CommentDetailsVO();
            //查询该评论的点赞数
            Integer likeCount = postDetailsMapper.likeCount(tlDetail.getDetailsId());
            //查询当前用户是否点赞该评论
            Boolean flag = postDetailsMapper.flag(tlDetail.getDetailsId(),userId);
            commentDetailsVO.setComments(tlDetail.getComments());
            commentDetailsVO.setDetailsId(tlDetail.getDetailsId());
            commentDetailsVO.setAddress(tlDetail.getAddress());
            commentDetailsVO.setRecoveryTime(tlDetail.getRecoveryTime());
            commentDetailsVO.setPostId(tlDetail.getPostId());
            commentDetailsVO.setLikeCount(tlDetail.getLikeCount());
            commentDetailsVO.setFlag(flag);
            commentDetailsVO.setUserName(user.getUsername());
            commentDetailsVO.setUserImage(user.getUserImage());
            list.add(commentDetailsVO);
        }
        return list;
    }

    @Override
    public Boolean add(Integer detailsId, Integer userId) {
        Integer i = postDetailsMapper.likeCountAdd(detailsId);
        return postDetailsMapper.add(detailsId,userId)>0 && i>0;
    }

    @Override
    public Boolean delete(Integer detailsId, Integer userId) {
        Integer i = postDetailsMapper.likeCountDelete(detailsId);
        return postDetailsMapper.delete(detailsId,userId)>0 && i>0;
    }

    @Override
    public Boolean deleteFollow(Integer followId, Integer userId) {
        return postDetailsMapper.deleteFollow(followId,userId);
    }

    @Override
    public Boolean addFollow(Integer followId, Integer userId) {
        return postDetailsMapper.addFollow(followId,userId);
    }
}
